﻿#include<string.h>
#include<stdio.h>

void hex_str_to_byte(const char* hex_str, int length, unsigned char* result)
{
	char h, l;
	for (int i = 0; i < length / 2; i++)
	{
		if (*hex_str < 58)
		{
			h = *hex_str - 48;
		}
		else if (*hex_str < 71)
		{
			h = *hex_str - 55;
		}
		else
		{
			h = *hex_str - 87;
		}
		hex_str++;
		if (*hex_str < 58)
		{
			l = *hex_str - 48;
		}
		else if (*hex_str < 71)
		{
			l = *hex_str - 55;
		}
		else
		{
			l = *hex_str - 87;
		}
		hex_str++;
		*result++ = h << 4 | l;
	}
}

int main()
{
	const char* str = "pa!st17m@04h5u#0";
	unsigned char temp[16] = { 0 };
	hex_str_to_byte(str, strlen("pa!st17m@04h5u#0"), temp);
	for (int i = 0; i < 16; i++)
	{
		printf("%x ", temp[i]);
	}
	printf("\n");
	return 0;
}
/*#include <stdio.h>
#include "AES.h"

static int hex2byte(char* dst,const char* src) {
	while (*src) {
		if (' ' == *src) {
			src++;
			continue;
		}
		sscanf_s(src, "%02X", dst);
		src += 2;
		dst++;
	}
	return 0;
}

int main()
{
	const char* ckey = "0C B1 15 02 C0 27 01 20";
	char cDst[16];
	hex2byte(cDst, ckey);
	byte key[] = { 0x6E, 0x66, 0xAE, 0x0B,
		0xE7, 0x29, 0x41, 0xEB,
		0xA6, 0x53, 0x10, 0x21,
		0x13, 0x76, 0x08, 0x2E
	};
	printf("key %s\n", ckey);

	char src[] = "my test";
	printf("data before encryption. len = [%u]:\n", strlen(src));
	for (UINT i = 0; i < strlen(src); i++)
	{
		printf("0x%02X ", src[i]);
	}
	printf("\n\n");

	void* dst = 0;
	CAES aes(key);
	const UINT dst_len = aes.Encrypt(reinterpret_cast<void*>(src), sizeof(src), dst, 0);
	printf("encrypted data. len = [%u]:\n", dst_len);
	for (UINT i = 0; i < dst_len; i++)
	{
		printf("0x%02X ", reinterpret_cast<BYTE*>(dst)[i]);
	}
	printf("\n\n");

	aes.Decrypt(dst, dst_len);
	printf("decrypted data:\n");
	for (UINT i = 0; i < dst_len; i++)
	{
		printf("0x%02X", reinterpret_cast<BYTE*>(dst)[i]);
	}

	printf("\n");
	printf("%s\n", (const char *)dst);
	printf("\n\n");

	delete[]dst;

	return 0;
}*/